<template lang="pug">
.goods_scroll(v-if='show_flashSale != -1 && show')
  swiperRowBox(:clist="commodityArr")
</template>
<script>
import { pro } from '@/config/env'
import { unique } from '@/config/utils'
import fetch from '@/config/fetch'
import { getCommodityData } from '@/service/getData'
import swiperRowBox from './_swiper_row_box.vue'
import { mapGetters } from 'vuex'
export default {
  props: ['cdata'],
  data() {
    return {
      sdata: {},
      commodityIdBtcArr: [], // b2cids
      // commodityIdArr: [], // ids
      commodityArr: [], // 商品数组
      usefulArr: 0, // 有效商品位置
      maxnum: 20, // 商品最大数量
      show: true
    }
  },
  mounted() {
    if (this.show_flashSale != -1) {
      // 获取数据
      this.$store.commit('setShowTimeTitle',true)
      this.getData()
    }else {
      this.$store.commit('setShowTimeTitle',false)
    }
  },
  methods: {
    // 获取限时抢购商品信息
    async getCommodityList() {
      let p = this.$store.state.position
      let commoditydata = await getCommodityData(p.cityCode, p.storeLocation, unique(this.commodityIdBtcArr))
      if (commoditydata.resultMsg == "成功") {
        let arr = []
        commoditydata.resultData.forEach(function (item, index) {
          item.per = this.getPerById(item.cmmdtyCode)
          arr.push(item)
        }, this)
        this.commodityArr = arr.splice(0, this.maxnum)
      }
    },
    getData() {
      this.cdata.forEach(function (item) {
        if (item.sequence == this.show_flashSale) {
          this.sdata = item
        }
      }, this)
      if (!this.sdata.tag) {
        this.$store.commit('setShowTimeTitle',false)
        this.show = false
        return
      }
      if (this.sdata.tag.length == 0 ) {
        this.$store.commit('setShowTimeTitle',false)
        this.show = false
        return
      }
      this.getCommodityIdBtcArr()
      // this.getCommodityIdArr()
      this.getCommodityList()
    },
    // 获取b2cids
    getCommodityIdBtcArr() {
      let arr = []
      if (!this.sdata.tag) {
        this.$nextTick(()=> {
          this.$store.commit('setShowFlashSale',-1)
          this.$store.commit('setShowTimeTitle',false)
        })
        return
      }
      if (this.sdata.tag[0].productSpecialFlag) {
        this.maxnum = this.sdata.tag[0].productSpecialFlag.split('_')[0]
      }
      this.sdata.tag.forEach(function (item) {
        arr.push(`${item.elementDesc || '0000000000'}-${item.elementName}`)
      })
      if (arr.length == 0) {
        this.$store.commit('setShowTimeTitle',false)
        this.$nextTick(()=> {
          this.$store.commit('setShowFlashSale',-1)
        })
        return
      }
      this.commodityIdBtcArr = arr
    },
    // getCommodityIdArr() {
    //   let arr = []
    //   this.cdata[0].tag.forEach(function (item) {
    //     arr.push(item.elementName)
    //   })
    //   this.commodityIdArr = unique(arr)
    // },
    getPerById(id) {
      let per = ''
      this.sdata.tag.forEach(function (item) {
        if (item.elementName == id) {
          per = item.productSpecialFlag
        }
      }, this)
      return per
    }
  },
  components: {
    swiperRowBox
  },
  computed: {
    ...mapGetters([
      'show_flashSale'
    ])
  },
  watch: {
    'show_flashSale': function () {
      if (this.show_flashSale != -1) {
        // 获取数据
        this.$store.commit('setShowTimeTitle',true)
        this.getData()
      }else {
        this.$store.commit('setShowTimeTitle',false)
      }
    } 
  }
}
</script>

<style lang="scss" scoped>
@import './tool.scss';
.goods_scroll {
  background-color: #fff;
}
</style>
